# coding: UTF-8
# Filename : server.py.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-

import socket,threading



class HttpThread(threading.Thread):
    def __init__(self, connection, address):
        threading.Thread.__init__(self)
        self.connection = connection
        self.address = address
    def run(self):
        print self.connection
        print self.address
        try:  
            self.connection.settimeout(4)  
            buf = self.connection.recv(1024)
            str = buf
            if buf == 'hello':  
                self.connection.send(str + 'welcome to server!')  
            else:  
                self.connection.send(str + '<div style="font-size:12px">welcome to my python server!</div>')  
        except socket.timeout:  
            print 'time out'  
        self.connection.close()

class HttpServer(threading.Thread):
    def __init__(self, i):
        threading.Thread.__init__(self)
        self.i = i
    def run(self):
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
        sock.bind(('www.lmapp.net', 79+self.i))  
        sock.listen(10000)
        count = 1
        while True:
            connection,address = sock.accept()
            print count
            count += 1
            httpThread = HttpThread(connection,address)
            httpThread.start()    

if __name__ == '__main__':

    for i in range(1,6):
        httpServer = HttpServer(i)
        httpServer.start()
        




